package ru.fsd.sokoban.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.MenuItem;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import ru.fsd.sokoban.GameBoard;
import ru.fsd.sokoban.GameBoardKeyListener;
import ru.fsd.sokoban.GameObject;
import ru.fsd.sokoban.LevelLoader;
import ru.fsd.sokoban.MenuItemActionListener;


public class GameFrame extends JFrame {
	
	private GameBoard board;
	private GameBoardPanel gamePanel = null;
	private JMenuItem miAbout;
	private JMenuItem miOpenLevel;
	private JMenuItem miReloadLevel;
	private GameBoardKeyListener keyListener;
	private String pathToLevel = "data/levels/level1.fsd";
	private ArrayList<Integer> actions = null;
	private JLabel statusBar = null;
	
	public GameFrame() {
		this("Sokoban");
	}
	
	public GameFrame(String title) {
		super(title);
		init();
	}
	
	public void addAction(int act)
	{
		actions.add(act);
	}
	
	public int getAction()
	{
		if (actions.isEmpty()) {
			return 20;
		}
		else {
			int act = actions.get(actions.size()-1);
			actions.remove(actions.size()-1);
			return act;			
		}
	}
	
	public GameBoard getGameBoard() {
		return board;
	}
	
	public GameBoardPanel getGameBoardPanel() {
		return gamePanel;
	}
	
	private void init() {
		setLayout(new BorderLayout());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		addMenuBar();
		initStatusBar();
		
		actions = new ArrayList<Integer>();
		
		reloadLevel(pathToLevel);
		add(gamePanel);
		
		
		
		
		keyListener = new GameBoardKeyListener(this);
		addKeyListener(keyListener);
		
		pack();
		setVisible(true);
	}
	
	public static void main(String[] args) {
		new GameFrame();
	}
	
	public void reloadLevel(String pathToFile) {
		this.pathToLevel = pathToFile;
		LevelLoader loader = new LevelLoader();
		
		try {
			board = loader.loadBoard(pathToFile);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "IO ERROR!!!", "Error", JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
			return;
		}
		if(gamePanel == null) {
			gamePanel = new GameBoardPanel(board);
			keyListener = new GameBoardKeyListener(this);
		}
		gamePanel.setGameBoard(board);
		keyListener.updateBoard();
		pack();
		
		actions = new ArrayList<Integer>();
		refreshStatusBar();
	}
	
	public void reloadLevel() {
		LevelLoader loader = new LevelLoader();
		
		try {
			board = loader.loadBoard(pathToLevel);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "IO ERROR!!!", "Error", JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
			return;
		}
		if(gamePanel == null) {
			gamePanel = new GameBoardPanel(board);
			keyListener = new GameBoardKeyListener(this);
		}
		gamePanel.setGameBoard(board);
		keyListener.updateBoard();
		pack();
		
		actions = new ArrayList<Integer>();
		refreshStatusBar();
	}
	
	private void addMenuBar() {
		JMenuBar menuBar = new JMenuBar();
		MenuItemActionListener actionListener = new MenuItemActionListener(this);
		
		JMenu mFile = new JMenu("File");
		JMenu mHelp = new JMenu("Help");
		
		miOpenLevel = new JMenuItem("Open level");
		miOpenLevel.addActionListener(actionListener);
		mFile.add(miOpenLevel);
		
		miReloadLevel = new JMenuItem("Reload level");
		miReloadLevel.addActionListener(actionListener);
		mFile.add(miReloadLevel);
		
		miAbout = new JMenuItem("About");
		miAbout.addActionListener(actionListener);
		mHelp.add(miAbout);
		
		menuBar.add(mFile);
		menuBar.add(mHelp);
		setJMenuBar(menuBar);
	}
	
	private void initStatusBar() {
		statusBar = new JLabel("Step: 0");
		JPanel statusPanel = new JPanel();
		statusPanel.add(statusBar);
		add(statusPanel, BorderLayout.SOUTH);
	}
	
	public void refreshStatusBar() {
		statusBar.setText("Step: " + actions.size());
	}
	
	public void showGameOver() {
		JOptionPane.showMessageDialog(this, "You win!!!", "Game Over", JOptionPane.INFORMATION_MESSAGE);
	}
}
